package com.zzj.filedownloader.download;

import android.content.Context;
import com.zzj.filedownloader.download.DownLoader;
import com.zzj.filedownloader.download.dbcontrol.DataKeeper;
import com.zzj.filedownloader.download.dbcontrol.FileHelper;
import com.zzj.filedownloader.download.dbcontrol.bean.SQLDownLoadInfo;
import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/* loaded from: classes.dex */
public class DownLoadManager {
    private DownLoadListener alltasklistener;
    private Context mycontext;
    private ThreadPoolExecutor pool;
    private ArrayList<DownLoader> taskList = new ArrayList<>();
    private final int MAX_DOWNLOADING_TASK = 5;
    private DownLoader.DownLoadSuccess downloadsuccessListener = null;
    private boolean isSupportBreakpoint = false;

    public DownLoadManager(Context context) {
        this.mycontext = context;
        init(context);
    }

    private void init(Context context) {
        this.pool = new ThreadPoolExecutor(5, 5, 30L, TimeUnit.SECONDS, new ArrayBlockingQueue(2000));
        this.downloadsuccessListener = new DownLoader.DownLoadSuccess() { // from class: com.zzj.filedownloader.download.DownLoadManager.1
            @Override // com.zzj.filedownloader.download.DownLoader.DownLoadSuccess
            public void onTaskSeccess(String str) {
                int size = DownLoadManager.this.taskList.size();
                for (int i = 0; i < size; i++) {
                    DownLoader downLoader = (DownLoader) DownLoadManager.this.taskList.get(i);
                    if (downLoader.getTaskID().equals(str)) {
                        DownLoadManager.this.taskList.remove(downLoader);
                        return;
                    }
                }
            }
        };
        recoverData(this.mycontext);
    }

    private void recoverData(Context context) {
        stopAllTask();
        this.taskList = new ArrayList<>();
        ArrayList<SQLDownLoadInfo> allDownLoadInfo = new DataKeeper(context).getAllDownLoadInfo();
        if (allDownLoadInfo.size() > 0) {
            int size = allDownLoadInfo.size();
            for (int i = 0; i < size; i++) {
                DownLoader downLoader = new DownLoader(context, allDownLoadInfo.get(i), this.pool, this.isSupportBreakpoint, false);
                downLoader.setDownLodSuccesslistener(this.downloadsuccessListener);
                downLoader.setDownLoadListener("public", this.alltasklistener);
                this.taskList.add(downLoader);
            }
        }
    }

    public void setSupportBreakpoint(boolean z) {
        if (!this.isSupportBreakpoint && z) {
            int size = this.taskList.size();
            for (int i = 0; i < size; i++) {
                this.taskList.get(i).setSupportBreakpoint(true);
            }
        }
        this.isSupportBreakpoint = z;
    }

    public int addTask(String str, String str2, String str3, String str4) {
        if (str == null) {
            str = str3;
        }
        int attachmentState = getAttachmentState(str, str3, str4);
        if (attachmentState != 1) {
            return attachmentState;
        }
        SQLDownLoadInfo sQLDownLoadInfo = new SQLDownLoadInfo();
        sQLDownLoadInfo.setDownloadSize(0L);
        sQLDownLoadInfo.setFileSize(0L);
        sQLDownLoadInfo.setTaskID(str);
        sQLDownLoadInfo.setFileName(str3);
        sQLDownLoadInfo.setUrl(str2);
        sQLDownLoadInfo.setFilePath(str4);
        DownLoader downLoader = new DownLoader(this.mycontext, sQLDownLoadInfo, this.pool, this.isSupportBreakpoint, true);
        downLoader.setDownLodSuccesslistener(this.downloadsuccessListener);
        if (this.isSupportBreakpoint) {
            downLoader.setSupportBreakpoint(true);
        } else {
            downLoader.setSupportBreakpoint(false);
        }
        downLoader.start();
        downLoader.setDownLoadListener("public", this.alltasklistener);
        this.taskList.add(downLoader);
        return 1;
    }

    private int getAttachmentState(String str, String str2, String str3) {
        int size = this.taskList.size();
        for (int i = 0; i < size; i++) {
            if (this.taskList.get(i).getTaskID().equals(str)) {
                return 0;
            }
        }
        if (str3 != null) {
            return new File(str3).exists() ? -1 : 1;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(FileHelper.getFileDefaultPath());
        sb.append("/(");
        sb.append(FileHelper.filterIDChars(str));
        sb.append(")");
        sb.append(str2);
        return new File(sb.toString()).exists() ? -1 : 1;
    }

    public void deleteTask(String str) {
        int size = this.taskList.size();
        for (int i = 0; i < size; i++) {
            DownLoader downLoader = this.taskList.get(i);
            if (downLoader.getTaskID().equals(str)) {
                downLoader.destroy();
                this.taskList.remove(downLoader);
                return;
            }
        }
    }

    public ArrayList<String> getAllTaskID() {
        ArrayList<String> arrayList = new ArrayList<>();
        int size = this.taskList.size();
        for (int i = 0; i < size; i++) {
            arrayList.add(this.taskList.get(i).getTaskID());
        }
        return arrayList;
    }

    public ArrayList<TaskInfo> getAllTask() {
        ArrayList<TaskInfo> arrayList = new ArrayList<>();
        int size = this.taskList.size();
        for (int i = 0; i < size; i++) {
            DownLoader downLoader = this.taskList.get(i);
            SQLDownLoadInfo sQLDownLoadInfo = downLoader.getSQLDownLoadInfo();
            TaskInfo taskInfo = new TaskInfo();
            taskInfo.setFileName(sQLDownLoadInfo.getFileName());
            taskInfo.setOnDownloading(downLoader.isDownLoading());
            taskInfo.setTaskID(sQLDownLoadInfo.getTaskID());
            taskInfo.setFileSize(sQLDownLoadInfo.getFileSize());
            taskInfo.setDownFileSize(sQLDownLoadInfo.getDownloadSize());
            arrayList.add(taskInfo);
        }
        return arrayList;
    }

    public void startTask(String str) {
        int size = this.taskList.size();
        for (int i = 0; i < size; i++) {
            DownLoader downLoader = this.taskList.get(i);
            if (downLoader.getTaskID().equals(str)) {
                downLoader.start();
                return;
            }
        }
    }

    public void stopTask(String str) {
        int size = this.taskList.size();
        for (int i = 0; i < size; i++) {
            DownLoader downLoader = this.taskList.get(i);
            if (downLoader.getTaskID().equals(str)) {
                downLoader.stop();
                return;
            }
        }
    }

    public void startAllTask() {
        int size = this.taskList.size();
        for (int i = 0; i < size; i++) {
            this.taskList.get(i).start();
        }
    }

    public void stopAllTask() {
        int size = this.taskList.size();
        for (int i = 0; i < size; i++) {
            this.taskList.get(i).stop();
        }
    }

    public void setSingleTaskListener(String str, DownLoadListener downLoadListener) {
        int size = this.taskList.size();
        for (int i = 0; i < size; i++) {
            DownLoader downLoader = this.taskList.get(i);
            if (downLoader.getTaskID().equals(str)) {
                downLoader.setDownLoadListener("private", downLoadListener);
                return;
            }
        }
    }

    public void setAllTaskListener(DownLoadListener downLoadListener) {
        this.alltasklistener = downLoadListener;
        int size = this.taskList.size();
        for (int i = 0; i < size; i++) {
            this.taskList.get(i).setDownLoadListener("public", downLoadListener);
        }
    }

    public void removeDownLoadListener(String str) {
        DownLoader downloader = getDownloader(str);
        if (downloader != null) {
            downloader.removeDownLoadListener("private");
        }
    }

    public void removeAllDownLoadListener() {
        int size = this.taskList.size();
        for (int i = 0; i < size; i++) {
            this.taskList.get(i).removeDownLoadListener("public");
        }
    }

    public boolean isTaskdownloading(String str) {
        DownLoader downloader = getDownloader(str);
        if (downloader != null) {
            return downloader.isDownLoading();
        }
        return false;
    }

    private DownLoader getDownloader(String str) {
        for (int i = 0; i < this.taskList.size(); i++) {
            DownLoader downLoader = this.taskList.get(i);
            if (str != null && str.equals(downLoader.getTaskID())) {
                return downLoader;
            }
        }
        return null;
    }

    public TaskInfo getTaskInfo(String str) {
        SQLDownLoadInfo sQLDownLoadInfo;
        DownLoader downloader = getDownloader(str);
        if (downloader == null || (sQLDownLoadInfo = downloader.getSQLDownLoadInfo()) == null) {
            return null;
        }
        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setFileName(sQLDownLoadInfo.getFileName());
        taskInfo.setOnDownloading(downloader.isDownLoading());
        taskInfo.setTaskID(sQLDownLoadInfo.getTaskID());
        taskInfo.setDownFileSize(sQLDownLoadInfo.getDownloadSize());
        taskInfo.setFileSize(sQLDownLoadInfo.getFileSize());
        return taskInfo;
    }
}
